package com.hqyj.finance.server.invest.financeinvest.service.impl;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.http.HttpUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqyj.finance.server.invest.financeinvest.entity.StockIndexs;
import com.hqyj.finance.server.invest.financeinvest.mapper.StockIndexsMapper;
import com.hqyj.finance.server.invest.financeinvest.service.StockIndexsService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class StockIndexsServiceImpl extends ServiceImpl<StockIndexsMapper, StockIndexs> implements StockIndexsService {

    @Value("${myproject.ig507-licence}")
    private String licence;

    @Resource
    private StockIndexsMapper stockIndexsMapper;

    @Override
    public boolean addStockIndexs(StockIndexs stockIndexs) throws Exception {
        return super.save(stockIndexs);
    }

    @Override
    public String queryStockIndexRealInfoByCode(String code) throws Exception {

        return HttpUtil.get("https://ig507.com/data/time/real/" + code + "?licence="+this.licence, CharsetUtil.CHARSET_UTF_8);
    }

    @Override
    public String queryAllStockIndexRealInfo() throws Exception {
        List<StockIndexs> stockIndexsList = super.list();
        StringBuffer sb = new StringBuffer("[");
        for (StockIndexs stockIndexs : stockIndexsList) {
            sb.append("{ \"dm\": \"").append(stockIndexs.getDm())
                    .append("\",\"mc\":\""+stockIndexs.getMc())
                    .append("\",\"info\":")
                    .append(queryStockIndexRealInfoByCode(stockIndexs.getDm())).append("},");

        }
        sb.deleteCharAt(sb.length()-1);
        sb.append("]");
        return sb.toString();
    }

    @Override
    public String queryAllStockSectorRealInfo() throws Exception {
        return HttpUtil.get("https://ig507.com/data/all/zjlx/zjhhy?licence="+this.licence, CharsetUtil.CHARSET_UTF_8);
    }


}
